import { hasToken } from '@/utils/token'
import { ReactNode } from 'react'
import { Redirect, Route, RouteProps } from 'react-router-dom'

const PrivateRoute = ({ children, ...rest }: RouteProps) => {
  // console.log({ children, ...rest })
  return (
    // {...rest}传与不传有关系吗？
    <Route
      {...rest}
      render={(props) => {
        if (hasToken()) {
          return children as ReactNode
        }
        // 否则重定向跳转到登录页，并且携带上当前页面的路径，以便登录后直接跳转
        return (
          <Redirect
            to={{
              pathname: '/login',
              state: {
                from: props.location.pathname
              }
            }}
          />
        )
      }}
    />
  )
}

export default PrivateRoute
